package com.zqxq.lottery.controller.analysis;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.DateUtils;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.common.utils.CommonUtil;
import com.zqxq.lottery.entity.po.analysis.*;
import com.zqxq.lottery.entity.po.jczq.JczqMatchLivePO;
import com.zqxq.lottery.service.analysis.*;
import com.zqxq.lottery.service.jczq.IJczqMatchResultService;
import com.zqxq.lottery.util.NumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class AnalysisController extends AnalysisBaseController {

    @Autowired
    private IAnalysisEventService analysisEventService;
    @Autowired
    private IAnalysisLineupService analysisFirstLineupService;
    @Autowired
    private IAnalysisForecastService analysisForecastService;
    @Autowired
    private IJczqMatchResultService jczqMatchResultService;
    @Autowired
    private IAnalysisOddsService analysisOddsService;
    @Autowired
    private IAnalysisPointService analysisPointService;
    @Autowired
    private IAnalysisRecordService analysisRecordService;
    @Autowired
    private IAnalysisVoteService analysisVoteService;

    @PostMapping("/event/get")
    public Result getAnalysisEvent(@RequestParam String code) {
        Result rs = new Result();
        try {
            List<AnalysisEventPO> list = analysisEventService.get(code);
            rs.setData(list);
        } catch (Exception e) {
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("get AnalysisEvent Error:", e);
        }
        return rs;
    }

    @PostMapping("/lineup/get")
    public Result getAnalysisLineup(@RequestParam String code) {
        Result rs = new Result();
        try {
            List<AnalysisLineupPO> list = analysisFirstLineupService.get(code);
            rs.setData(list);
        } catch (Exception e) {
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("get FirstLineup Error:", e);
        }
        return rs;
    }

    @PostMapping("/forecast/get")
    public Result getAnalysisForecast(@RequestParam String code) {
        Result rs = new Result();
        try {
            Wrapper<AnalysisForecastPO> wrapper = new EntityWrapper<>();
            wrapper.eq("code",code);
            AnalysisForecastPO analysisForecastPO = analysisForecastService.selectOne(wrapper);
            rs.setData(analysisForecastPO);
        } catch (Exception e) {
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("get Forecast Error:", e);
        }
        return rs;
    }

    @PostMapping("/match/get")
    public Result<JczqMatchLivePO> getAnalysisMatch(@RequestParam String code) {
        Result<JczqMatchLivePO> rs = new Result();
        try {
            Wrapper<JczqMatchLivePO> wrapper = new EntityWrapper<>();
            wrapper.eq("match_id",code);
            JczqMatchLivePO jczqMatchLivePO = jczqMatchResultService.selectOne(wrapper);
            if (jczqMatchLivePO != null && StrUtils.isNotEmpty(jczqMatchLivePO.getHost_team_top()) && StrUtils.isNotEmpty(jczqMatchLivePO.getGuest_team_top())){
                jczqMatchLivePO.setHost_team_top(NumUtil.getNum(jczqMatchLivePO.getHost_team_top()));
                jczqMatchLivePO.setGuest_team_top(NumUtil.getNum(jczqMatchLivePO.getGuest_team_top()));
            }
            rs.setData(jczqMatchLivePO);
        } catch (Exception e) {
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("get Match Error:", e);
        }
        return rs;
    }

    /**
     * 作者:    
     * 创建时间: 2018年7月23日下午16:09:38
     * 描述:    赛事分析-赔率信息查询
     * 备注:
     *
     * @param code      赛事编号
     * @param odds_type 赔率类型(0-亚盘,1-欧赔,2-大小球)
     * @return
     */
    @PostMapping("/odds/get")
    public Result getAnalysisOdds(@RequestParam("code") String code, @RequestParam("odds_type") Integer odds_type) {
        log.info("赛事分析-赔率信息查询入参,code:{},odds_type:{}", code, odds_type);
        Result rs = new Result();
        try {
            Wrapper<AnalysisOddsPO> wrapper = new EntityWrapper();
            wrapper.eq("code", code);
            wrapper.eq("odds_type", odds_type);
            List<AnalysisOddsPO> list = analysisOddsService.selectList(wrapper);
            rs.setData(list);
        } catch (Exception e) {
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("get Odds Error:", e);
        }
        return rs;
    }

    /**
     * 作者:    
     * 创建时间: 2018年7月23日下午16:09:38
     * 描述:    赛事分析-比賽积分信息查询
     * 备注:
     *
     * @param code       赛事编号
     * @param point_type 积分类型 0-总积分；1-主场积分；2-客场积分
     * @return
     */
    @PostMapping("/point/get")
    public Result getAnalysisPoint(@RequestParam String code, @RequestParam("point_type") Integer point_type) {
        Result rs = new Result();
        try {
            Wrapper<AnalysisPointPO> wrapper = new EntityWrapper<>();
            wrapper.eq("code", code);
            wrapper.eq("point_type", point_type);
            List<AnalysisPointPO> list = analysisPointService.selectList(wrapper);
            rs.setData(list);
        } catch (Exception e) {
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("get Point Error:", e);
        }
        return rs;
    }

    /**
     * 作者:    
     * 创建时间: 2018年7月23日下午16:09:38
     * 描述:    赛事分析-分析查询
     * 备注:
     *
     * @param code    赛事编号
     * @param user_id 用户编号
     * @return
     */
    @PostMapping("/record/get")
    public Result getAnalysisRecord(@RequestParam("code") String code, @RequestParam(value = "user_id",required = false) Long user_id) {
        log.info("赛事分析-分析查询入参,code:{},user_id:{}", code, user_id);
        Result rs = new Result();
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("recordPOList", analysisRecordService.get(code));
            Map<String, Object> voteMap = analysisVoteService.getVoteByUserId(code, user_id);
            result.put("votePOList", voteMap);
            rs.setData(result);
        } catch (Exception e) {
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("get Record Error:", e);
        }
        return rs;
    }

    /**
     * 作者:    
     * 创建时间: 2018年7月23日下午16:09:38
     * 描述:    赛事分析-分析-投票
     * 备注:
     *
     * @param code      赛事编号
     * @param user_id   用户编号
     * @param vote_type 投票类型 0-主胜；1-平；3-客胜；
     * @return
     */
    @PostMapping("/vote/add")
    public Result<?> addAnalysisVote(@RequestParam("code") String code, @RequestParam("user_id") Long user_id, @RequestParam("vote_type") Integer vote_type) {
        log.info("赛事分析-分析-投票入参,code:{},user_id:{},vote_type:{}", code, user_id, vote_type);
        try {
            Wrapper<AnalysisVotePO> wrapper = new EntityWrapper<>();
            wrapper.eq("user_id", user_id);
            wrapper.eq("code", code);
            AnalysisVotePO po = analysisVoteService.selectOne(wrapper);
            boolean flag;
            if (CommonUtil.isEmpty(po)) {
                po = new AnalysisVotePO();
                po.setCode(code);
                po.setUser_id(user_id);
                po.setVote_type(vote_type);
                flag = analysisVoteService.insert(po);
            } else {
                flag = true;
            }
            if (flag) {
                return new Result<>(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMsg(),analysisVoteService.getVoteByUserId(code, user_id));
            } else {
                return new Result<>(CodeEnum.FAIL.getCode(), CodeEnum.FAIL.getMsg());
            }
        } catch (Exception e) {
            log.error("get Record Error:", e);
            return new Result<>(CodeEnum.FAIL.getCode(), CodeEnum.FAIL.getMsg());
        }
    }
}
